package com.czh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czh.domain.ResponseResult;
import com.czh.domain.dto.ChangeRoleStatusDto;
import com.czh.domain.dto.QueryRoleDto;
import com.czh.domain.dto.RoleDto;
import com.czh.domain.entity.Role;
import com.czh.domain.entity.RoleMenu;
import com.czh.domain.vo.RolePageVo;
import com.czh.domain.vo.RoleVo;
import com.czh.mapper.RoleMapper;
import com.czh.service.RoleMenuService;
import com.czh.service.RoleService;
import com.czh.utils.BeanCopyPlusUtils;
import com.czh.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2024-03-28 20:53:33
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMenuService roleMenuService;
    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        if(id == 1){
            List<String> roleList = new ArrayList<>();
            roleList.add("admin");
            return roleList;
        }
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult<RolePageVo> getRoleLst(Integer pageNum, Integer pageSize, QueryRoleDto userDto) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(userDto.getRoleName()), Role::getRoleName, userDto.getRoleName()).
                eq(StringUtils.hasText(userDto.getStatus()), Role::getStatus, userDto.getStatus());
        queryWrapper.orderByAsc( Role::getRoleSort);
        Page<Role> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        List<RoleVo> rolePageVos = BeanCopyUtils.copyBeanList(page.getRecords(), RoleVo.class);
        return ResponseResult.okResult(new RolePageVo(rolePageVos, page.getTotal()));
    }
    /**
     * 这是新增用户界面的角色展示
     */
    @Override
    public ResponseResult<List<Role>>  getAllRoleList() {
        return ResponseResult.okResult(list());
    }
    @Override
    public ResponseResult<Object> changeStatus(ChangeRoleStatusDto changeRoleStatusDto) {
        updateById(getById(changeRoleStatusDto.getRoleId()).setStatus(changeRoleStatusDto.getStatus()));
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Object> addRole(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        save(role);
        System.out.println(role.getId());
        List<RoleMenu> collect = roleDto.getMenuIds().stream().map(menuId -> new RoleMenu(role.getId(), menuId)).collect(Collectors.toList());
        roleMenuService.saveBatch(collect);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Object> deleteRole(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<RoleVo> getRoleById(Long id) {
        RoleVo roleVo = BeanCopyPlusUtils.copyProperties(getById(id), RoleVo.class);
        return ResponseResult.okResult(roleVo);
    }

    @Override
    public ResponseResult<Object> updateRole(RoleDto roleDto) {
        Role role = BeanCopyPlusUtils.nativeCopyProperties(roleDto, Role.class);
        updateById(role);

        LambdaQueryWrapper<RoleMenu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.eq(RoleMenu::getRoleId,roleDto.getId());
        roleMenuService.remove(menuLambdaQueryWrapper);

        if (roleDto.getMenuIds()!=null){
            List<RoleMenu> newRoleMenu = roleDto.getMenuIds().stream().
                    map(menuId -> new RoleMenu(roleDto.getId(), menuId)).collect(Collectors.toList());
            roleMenuService.saveBatch(newRoleMenu);
        }
        return ResponseResult.okResult();
    }

}

